package com.gome.ocean.controller.gddl.app.group;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gome.ocean.common.utils.ResponsePage;
import com.gome.ocean.dao.model.gddl.AppDatasourceBO;
import com.gome.ocean.dao.model.gddl.GroupBO;
import com.gome.ocean.dao.model.gddl.GroupRuleBO;
import com.gome.ocean.dao.model.gddl.GroupRuleVO;
import com.gome.ocean.service.base.BaseController;
import com.gome.ocean.service.gddl.AppDatasourceService;
import com.gome.ocean.service.gddl.GroupRuleService;
import com.gome.ocean.service.gddl.GroupService;
import com.google.common.collect.Lists;

/**
 * Created by liuqingxia on 2016/3/24.
 */
@Controller
@RequestMapping("/tddl/groupRuleVO")
public class GroupRuleVOController extends BaseController {

    private static final Logger  LOG = LoggerFactory.getLogger(GroupRuleVOController.class);
    @Autowired
    private GroupService         groupService;
    @Autowired
    private GroupRuleService     groupRuleService;
    @Autowired
    private AppDatasourceService appDatasourceService;

    @RequestMapping("/findByAppId")
    @ResponseBody
    public ResponsePage findByAppId(@RequestParam("appId") long appId) {
        ResponsePage page = getResponsePage();
        List<GroupRuleVO> groupRuleVOs = null;
        try {
            List<GroupBO> groupBOs = groupService.findGroupBOByAppId(appId);
            if (null != groupBOs || !groupBOs.isEmpty()) {
                groupRuleVOs = getAllGroupRuleVOS(groupBOs);
            }
            if (null == groupRuleVOs || groupRuleVOs.isEmpty()) {
                return page.emptyPage("未查询到结果");
            }
            return page.setJustList(groupRuleVOs, "查询成功");
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            return page.setErrorMsg(e.getMessage());
        }
    }

    private List<GroupRuleVO> getAllGroupRuleVOS(List<GroupBO> groupBOs) throws InvocationTargetException,
                                                                         IllegalAccessException {
        List<GroupRuleVO> allGroupRuleVOs = Lists.newArrayList();
        for (GroupBO groupBO : groupBOs) {
            List<GroupRuleBO> singleGroupRuleBOs = groupRuleService.findGroupRuleBOByGroupId(groupBO.getId());
            if (null != singleGroupRuleBOs && !singleGroupRuleBOs.isEmpty()) {
                List<GroupRuleVO> singleGroupRuleVOs = transformRuleBO2VO(singleGroupRuleBOs, groupBO.getGroupName());
                allGroupRuleVOs.addAll(singleGroupRuleVOs);
            }
        }
        return allGroupRuleVOs;
    }

    private List<GroupRuleVO> transformRuleBO2VO(List<GroupRuleBO> groupRuleBOs,
                                                 String groupName) throws InvocationTargetException,
                                                                   IllegalAccessException {
        List<GroupRuleVO> groupRuleVOs = Lists.newArrayList();
        for (GroupRuleBO groupRuleBO : groupRuleBOs) {
            GroupRuleVO groupRuleVO = new GroupRuleVO();
            groupRuleVO.setGroupName(groupName);
            BeanUtils.copyProperties(groupRuleVO, groupRuleBO);
            groupRuleVOs.add(setDataSourceStatus(groupRuleVO));
        }
        return groupRuleVOs;
    }

    private GroupRuleVO setDataSourceStatus(GroupRuleVO groupRuleVO) {
        long dataSourceId = groupRuleVO.getDatasourceId();
        AppDatasourceBO appDatasourceBO = appDatasourceService.findAppDatasourceBOById(dataSourceId);
        Integer status = appDatasourceBO.getSourceStatus();
        if (0 == status) {
            groupRuleVO.setDataSourceStatus("可用");
        } else {
            groupRuleVO.setDataSourceStatus("不可用");
        }
        return groupRuleVO;
    }

}
